package com.libgdxbar.pvzlikegame.objects;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Shape2D;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.libgdxbar.pvzlikegame.entities.MoveableEntity;
import org.libgdxbar.projects.interfaces.ICollision;
import org.libgdxbar.projects.interfaces.callback.CollisionCallback;
import org.libgdxbar.projects.interfaces.renderer.IShapeRender;

// 最简示例 僵尸类
// 继承 MoveableEntity 即可移动实体
// 实现 ShapeRenderable 接口 使用 ShapeRenderer 渲染图形
public class SampleShapeZombie extends MoveableEntity implements IShapeRender, ICollision {
	Rectangle shape;
	CollisionCallback collisionCallback = null;
	Array<ICollision> collisionEntities = null;
	int width = 50, height = 50;

	public SampleShapeZombie(float x, float y) {
		// 演示逻辑 创建随机方向移动的僵尸
		super(x, y, -MathUtils.random(400f) + 200f, -MathUtils.random(400f) + 200f);
		shape = new Rectangle(x, y, width, height);
		setCollisionCallback(shape -> {
			Rectangle rect = (Rectangle) shape;
			if (rect.x > this.shape.x) setVelocityX(-Math.abs(getVelocityX()));
			if (rect.x < this.shape.x) setVelocityX(Math.abs(getVelocityX()));
			if (rect.y > this.shape.y) setVelocityY(-Math.abs(getVelocityY()));
			if (rect.y < this.shape.y) setVelocityY(Math.abs(getVelocityY()));
		});
	}

	// 实现渲染方法
	@Override
	public void render(ShapeRenderer renderer) {
		Vector2 position = getPosition();
		renderer.setColor(Color.RED);
		renderer.rect(position.x, position.y, 50, 50);
	}

	// 实现僵尸逻辑
	@Override
	public void run(float delta) {
		super.run(delta);
		Vector2 position = getPosition();
		// 这里碰屏幕边缘反弹只是演示逻辑而已 实际请自己实现
		if (position.x > 640 - width) setVelocityX(-Math.abs(getVelocityX()));
		if (position.x < 0) setVelocityX(Math.abs(getVelocityX()));
		if (position.y > 480 - height) setVelocityY(-Math.abs(getVelocityY()));
		if (position.y < 0) setVelocityY(Math.abs(getVelocityY()));
		shape.setPosition(position.x, position.y);
		if (collisionEntities != null) {
			for (ICollision entity : collisionEntities) {
				entity.overlap(getShape());
			}
		}
	}

	public void setCollisionCallback(CollisionCallback callback) {
		this.collisionCallback = callback;
	}

	public void addCollisionEntity(ICollision entity) {
		if (collisionEntities == null) {
			collisionEntities = new Array<>();
		}
		collisionEntities.add(entity);
	}

	public void setCollisionEntities(Array<ICollision> entities) {
		this.collisionEntities = entities;
	}

	@Override
	public Shape2D getShape() {
		return shape;
	}

	@Override
	public boolean overlap(Shape2D shape) {
		// 限制只能和矩形碰撞检测
		// 其他种类碰撞检测如非 Gdx 自带的请自己实现算法
		// 不过应该是用不到的 谁不喜欢矩形呢
		if (this.shape != shape && shape instanceof Rectangle) {
			boolean isContains = this.shape.overlaps((Rectangle) shape);
			// 如果发生碰撞且回调函数存在就调用回调函数
			if (isContains && collisionCallback != null) {
				collisionCallback.onOverlap(shape);
			}
			return isContains;
		}
		// 其他种类都不检测 这不是 bug
		return false;
	}
}
